package com.foot.travel.viewholder;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

import com.foot.travel.R;
import com.koalac.ironman.common.adapter.ViewHolderModel;
import com.koalac.ironman.net.MyRequest;
import com.koalac.ironman.net.ResponseCallback;
import com.koalac.ironman.net.pojo.request.ClickNumber;
import com.koalac.ironman.net.pojo.request.DeleteNumber;
import com.koalac.ironman.net.pojo.response.HotRecommend;
import com.koalac.ironman.net.pojo.response.Response;
import com.koalac.ironman.util.ScreenInfoUtil;
import com.koalac.ironman.widget.ImageLoadAgreement.ImageType;
import com.koalac.ironman.widget.ScaleImageView;

public class HotRecommendViewHolder extends ViewHolderModel<HotRecommend>{

    TextView likeView;
    TextView poiView;
    ScaleImageView  imageView;
    
    public HotRecommendViewHolder(Context context, List<HotRecommend> list) {
	super(context, list);
    }

    @Override
    public void initViewHoler(int position, View convertView) {
	likeView = (TextView) convertView.findViewById(R.id.like_view);
	poiView = (TextView) convertView.findViewById(R.id.position);
	imageView = (ScaleImageView) convertView.findViewById(R.id.hot_recommend_image);
	RelativeLayout.LayoutParams layoutParams = (LayoutParams) imageView.getLayoutParams();
	layoutParams.height = ScreenInfoUtil.getScreenWidth(context)*9/16;
	imageView.setLayoutParams(layoutParams);
    }

    @Override
    public void setViewHolerValues(int position, View convertView) {
	final HotRecommend hotRecommend = list.get(position);
	likeView.setText(hotRecommend.getFollowNumber()+"人关注");
	poiView.setText(hotRecommend.getName());
	imageView.loadRoundImage(hotRecommend.getHeadImgUrl(), ImageType.SourceImage);
	
	if (hotRecommend.getNumber_flag() == HotRecommend.ATTENTION) {
	    likeView.setSelected(true);
	}else {
	    likeView.setSelected(false);
	}
	
	
	likeView.setOnClickListener(new OnClickListener() {
	    
	    @Override
	    public void onClick(View v) {
		 if (hotRecommend.getNumber_flag() == HotRecommend.ATTENTION) {
		     cancelAttention(hotRecommend);
		}else {
		    payAttention(hotRecommend);
		}
	    }
	});
    }
    /**
     * 关注
     * @param hotRecommend
     */
    private void payAttention(final HotRecommend hotRecommend) {
	ClickNumber clickNumber = new ClickNumber();
	clickNumber.setId(hotRecommend.getId());
	MyRequest.request(context, clickNumber, new ResponseCallback() {
	    
	    @Override
	    public void process(boolean result, Context context, Response data,
		    String msg) {
			if (result) {
			    int followNumber = Integer.parseInt(hotRecommend.getFollowNumber()) + 1;
			    hotRecommend.setFollowNumber(""+followNumber);
			    likeView.setText(hotRecommend.getFollowNumber()+"人关注");
			    likeView.setSelected(true);
			    hotRecommend.setNumber_flag(HotRecommend.ATTENTION);
			}
	    }
	});
    }
    /**
     * 取消关注
     * @param hotRecommend
     */
    private void cancelAttention(final HotRecommend hotRecommend) {
	DeleteNumber deleteNumber = new DeleteNumber();
	deleteNumber.setId(hotRecommend.getId());
	MyRequest.request(context, deleteNumber, new ResponseCallback() {
	    
	    @Override
	    public void process(boolean result, Context context, Response data,
		    String msg) {
			if (result) {
			     int followNumber = Integer.parseInt(hotRecommend.getFollowNumber()) -1;
			    hotRecommend.setFollowNumber(""+followNumber);
			    likeView.setText(hotRecommend.getFollowNumber()+"人关注");
			    likeView.setSelected(false);
			    hotRecommend.setNumber_flag(HotRecommend.NO_ATTENTION);
			}
	    }
	});
    }

}
